﻿Namespace Validation
    Public Class RegExValidationRule
        Inherits WPFGlue.Validation.ValidationRule


        Private _RegEx As System.Text.RegularExpressions.Regex
        Private _Expression As String
        Public Property Expression As String
            Get
                Return _Expression
            End Get
            Set(ByVal value As String)
                If value <> _Expression Then
                    _RegEx = New System.Text.RegularExpressions.Regex(value)
                    _Expression = value
                End If
            End Set
        End Property

        Public Sub New()
            Me.ValidationStep = System.Windows.Controls.ValidationStep.RawProposedValue
            Me.Message = "Must match the pattern {1}"
        End Sub
        Protected Overrides Function ValidateCore(ByVal value As Object, ByVal cultureInfo As System.Globalization.CultureInfo) As Boolean
            Dim result As Boolean = True
            If _RegEx IsNot Nothing Then
                result = _RegEx.IsMatch(value)
            End If
            Return result
        End Function

        Protected Overrides Function FormatMessage(ByVal value As Object) As String
            Return String.Format(Me.Message, value, Expression)
        End Function
    End Class
End Namespace
